
package formServlets;
import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreAttributes;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import javax.servlet.http.HttpServlet;

//import guestbook.PMF;

public class  UserPersonalInfoServlet  extends HttpServlet  {

	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		getServletContext().getRequestDispatcher("SNSP.html").forward(req, resp);
	}
 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Cookie cookies [] = request.getCookies ();
		Cookie myCookie = null;
		if (cookies != null)
		{
		for (int i = 0; i < cookies.length; i++)
		{
		if (cookies [i].getName().equals ("passUsername"))
		{
		myCookie = cookies[i];
		break;
		}
		}
		}
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		String firstname = request.getParameter("firstName");
		String lastname = request.getParameter("lastName");
		String gender = request.getParameter("gender");
		String citizenship = request.getParameter("citizenship");
		String Userid=myCookie.getValue();
		Entity user = new Entity("UserPersonalInfo",Userid);
		
		// check whether data already exist for some userid else it would be over written
		Query q = new Query("UserPersonalInfo");
		
		q.addFilter("Userid", Query.FilterOperator.EQUAL,Userid  );
		PreparedQuery pq = datastore.prepare(q);
		//System.out.println(FetchOptions.Builder.withPrefetchSize(1));
		
		int count=pq.countEntities(FetchOptions.Builder.withPrefetchSize(1));
		System.out.println("number of entities="+count);
		if(count >0)
		{// change year born to birthdate
		try {
			Key key = KeyFactory.createKey("UserPersonalInfo", Userid);
			Entity e=datastore.get(key);
			System.out.print("hjsdg"+e.getProperties());
			
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}}
		else
		{
			user.setProperty("Userid", Userid);
			user.setProperty("firstName", firstname);
			user.setProperty("lastName", lastname);
			user.setProperty("gender", gender);
			user.setProperty("citizenship", citizenship);
			datastore.put(user);
		
		}
		DatastoreAttributes ds=datastore.getDatastoreAttributes();
		DatastoreAttributes.DatastoreType dst= ds.getDatastoreType();
		System.out.print(dst.name());
		
		Query q1 = new Query();
		
		PreparedQuery pq1 = datastore.prepare(q1);
        
		for (Entity result : pq1.asIterable()) {
			System.out.println(result.getKind());
		  String firstName = (String) result.getProperty("firstName");
		  String lastName = (String) result.getProperty("lastName");
		  String citizen= (String) result.getProperty("citizenship");
		  String userName = (String) result.getProperty("Userid");
		  
		  System.out.println("User ID "+ userName + " Last name"+lastName + " First Name " + firstName+" citizen= "+citizen );
		}
		
		response.sendRedirect("successfulSignup.html");
 
	}
 
}
